<?php

class HmCurrencyController extends Fw_Controller_Action
{
	public function init()
	{
		$this->setDisplay('backoffice');
		$this->initSession();
	}

	public function indexAction()
	{
		$model = $this->_getModel('HmCurrency');
		$view_list = $this->_model;
		$rowset = $view_list->fetchAll();

		$rowCount = count($rowset);
		$this->view->view_list = $rowset;
		$this->view->rowCount  = $rowCount;
	}
	
	public function addAction()
	{
		if($this->category == 'A') {
			if ($this->_request->isPost()) {
				$formData = $this->_request->getPost('add');
				if ($formData) {
					
					$model = $this->_getModel('HmCurrency');
					$add   = $this->_model;
					
					$row        = $add->createRow();
					$row->name        = $this->_request->getPost('name');
					$row->description = $this->_request->getPost('description');
					$row->save();
					
					return $this->_helper->redirector('index');
				}
			}
		} else {
			return $this->_helper->redirector('hm-offer/index');
		}
	}
	
	public function editAction()
	{
		if($this->category == 'A') {
			$model   = $this->_getModel('HmCurrency');
			$edit    = $this->_model;
			$currency_id = (int)$this->_request->getParam('currency_id');
			
			if ($this->_request->isPost()) {
				$formData = $this->_request->getPost('edit');
				if ($formData) {
					$currency_id = (int)$this->_request->getPost('currency_id');
					$row = $edit->fetchRow('currency_id='.$currency_id);
	
					$row->name        = $this->_request->getPost('name');
					$row->description = $this->_request->getPost('description');
	
					$row->save();
					
					return $this->_helper->redirector('index');
				}
			}
			$this->view->edit = $edit->fetchRow('currency_id='.$currency_id);
		} else {
			return $this->_helper->redirector('hm-offer/index');
		}
	}
	
	public function deleteAction()
	{
		$currency_id = (int)$this->_request->getParam('currency_id');
		if($this->category == 'A') {
			if ($currency_id > 0) {
				$model = $this->_getModel('HmCurrency');
				$remove = $this->_model;
				
				$where = 'currency_id = ' . $currency_id;
				$remove->delete($where);
			}
		}
		return $this->_helper->redirector('index');
	}
}